﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace ciji.Core
{
    public class Util
    {
        
        /// <summary>
        /// 转换为int
        /// </summary>
        /// <param name="i"></param>
        /// <param name="fail">转换失败时，返回默认值</param>
        /// <returns></returns>
        public static int ConverToInt(object i, int fail = 0)
        {
            try
            {
                fail = Convert.ToInt32(i);
            }
            catch
            {
                return fail;
            }
            return fail;
        }

        // 时间戳转为C#格式时间
        public static DateTime StampToDateTime(string timeStamp)
        {
            DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);

            return dateTimeStart.Add(toNow);
        }

        // DateTime时间格式转换为Unix时间戳格式
        public static int DateTimeToStamp(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
        //// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        public static DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }

        public static string ReadFile(String filepath)
        {
            try
            {
                StreamReader sr = new StreamReader(filepath, System.Text.Encoding.Default);
                string temp = sr.ReadToEnd();
                sr.Close();
                return temp;
            }
            catch (IOException e)
            {
                return "";
            }

        }

        public static int Str2Int(string str,int def)
        {
            try
            {
                int rst = def;
                Int32.TryParse(str, out rst);
                return rst;
            }
            catch (Exception ex)
            {
                return def;
            }
        }
        public static int Str2Int(string str)
        {
            try
            {
                 
                Int32.TryParse(str, out int rst);
                return rst;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
   
        public static int Str2IntNotNull(string str)
        {
            try
            {

                Int32.TryParse(str, out int rst);
                return rst;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public static decimal Str2Decimal(string str)
        {
            try
            {

                decimal.TryParse(str, out decimal rst);
                return rst;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
         
        public static string GetLevelStr(int assessType,int level)
        {
            try
            {
                if (assessType == 0 || assessType == 1)
                {
                    if (level == 0)
                    {
                        return "能力完好";
                    }
                    else if (level == 1)
                    {
                        return "轻度失能";
                    }
                    else if (level == 2)
                    {
                        return "中度失能";
                    }
                    else if (level == 3)
                    {
                        return "重度失能";
                    }
                    else
                    {
                        return "未知状态";
                    }
                }
                else if (assessType == 2)
                {
                    if (level == 0)
                    {
                        return "能力完好";
                    }
                    else if (level == 1)
                    {
                        return "轻度失能";
                    }
                    else if (level == 2)
                    {
                        return "中度失能";
                    }
                    else if (level == 3)
                    {
                        return "重度失能";
                    }
                    else if (level == 4)
                    {
                        return "完全失能";
                    }
                    else
                    {
                        return "未知状态";
                    }
                }
                else {
                    if (level == 0)
                    {
                        return "自理";
                    }                   
                    else if (level == 2)
                    {
                        return "半自理";
                    }
                    else if (level == 3)
                    {
                        return "失能";
                    }
                    else
                    {
                        return "未知状态";
                    }
                }              
            }
            catch (Exception ex)
            {
                return "未知";
            }
        }

        public static string GetDBState(int? state)
        {
            try
            {
                 
                    if (state == 1)
                    {
                        return "符合";
                    }
                    else if (state == 2)
                    {
                        return "不符合";
                    }
                    else if (state == 3)
                    {
                        return "待复议";
                    }
                     
                    else
                    {
                        return "未知";
                    }
                 
            }
            catch (Exception ex)
            {
                return "未知";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d1">开始运营时间</param>
        /// <param name="d2">检查时间</param>
        /// <returns></returns>
        public static bool MoreOneYear(DateTime d1,DateTime d2) {

             
            if (d1.Year < d2.Year)
            {
                if (d2.Year - d1.Year > 1)
                {
                    return true;
                }
                else if (d1.Month < d2.Month)
                {
                    return true;
                }
                else if (d1.Month == d2.Month)
                {
                    if (d1.Date <= d2.Date)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}